//
//  HomeSearchViewController.m
//  YCSearchDemo
//
//  Created by huyuchen on 2018/3/3.
//  Copyright © 2018年 Hyc. All rights reserved.
//

#import "HomeSearchViewController.h"
#import "SearchNavBar.h"
#import "SearchResultCollectionView.h"
#import "SearchTableView.h"
#import "TitleTableViewCell.h"
#import "FactoryDetailViewController.h"
#import <FDFullscreenPopGesture/UINavigationController+FDFullscreenPopGesture.h>

#import "SearchModel.h"

@interface HomeSearchViewController ()
@property (nonatomic, strong) SearchNavBar *navBar;
@property (nonatomic, strong) SearchTableView *resultTableView;
@property (nonatomic, strong) NSMutableArray *resultArray;
@property (nonatomic, strong) SearchResultCollectionView *historyView;
@end

@implementation HomeSearchViewController

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = Color_Background;
    [self.view addSubview:self.navBar];
    [self.view addSubview:self.resultTableView];
    [self.view addSubview:self.historyView];
    
    [self.navBar.searchBar becomeFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//- (void)backToLastVc {
//    self.bm_popTransition = [BMSearchAnimationTool transition];
//    self.bm_popTransition.backOffsetY = 0;
//    [self.navigationController popViewControllerAnimated:YES];
//}

- (void)backToSuperView {
    [self.navigationController popViewControllerAnimated:YES];
}

//点击清除按钮 || 呼出键盘
- (void)stopSearchAction
{
    [self showHistoryView];
}

- (void)showHistoryView {
    self.resultTableView.hidden = YES;
    self.historyView.hidden = NO;
    self.navBar.cancelButton.hidden = NO;
}

- (void)hideHistoryView {
    self.resultTableView.hidden = NO;
    self.historyView.hidden = YES;
    self.navBar.cancelButton.hidden = YES;
}

#pragma mark - search
- (void)searchText:(NSString *)keyword {
    NSString *searchText = [keyword stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if ([searchText length] == 0) return;
    
    self.navBar.searchBar.text = keyword;
    [self.navBar.searchBar resignFirstResponder];
    [self loadResultData:searchText];
}

- (void)loadResultData:(NSString *)keyword {
    //添加历史记录
    [self.historyView appendResultHistoryText:keyword];
    
    [self hideHistoryView];
    
    [self.resultArray removeAllObjects];
    
    //网络请求
    BMWeakSelf
    [HttpRequest searchKeyword:keyword success:^(id object) {
        weakSelf.resultArray = [SearchModel mj_objectArrayWithKeyValuesArray:[object objectForKey:@"list"]];
        weakSelf.resultTableView.dataArray = weakSelf.resultArray;
        [weakSelf.resultTableView reloadData];
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
    }];
}

#pragma mark - 跳转
- (void)gotoWorkAreaDetailVc:(SearchModel *)model {
    FactoryDetailViewController *vc = [FactoryDetailViewController new];
    vc.areaid = model.areaid;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - getter
- (SearchNavBar *)navBar {
    if (!_navBar) {
        __weak typeof(self) weakSelf = self;
        _navBar = [[SearchNavBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64) beginEditBlock:^(UISearchBar *searchBar) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            [strongSelf stopSearchAction];
        } clickSearchBlock:^(UISearchBar *searchBar) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            [strongSelf searchText:searchBar.text];
        }];
        
        _navBar.backgroundColor = Color_NavBgColor;
        _navBar.searchBar.tintColor = ColorDefalutGray;
        [_navBar.cancelButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
        _navBar.clickCancelBlock = ^(){
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (!strongSelf.resultTableView.hidden) {
                strongSelf.navBar.searchBar.text = @"";
                [strongSelf showHistoryView];
            }
            else {
                [strongSelf hideHistoryView];
            }
            [strongSelf.navBar.searchBar resignFirstResponder];
            
        };
        _navBar.clickBackBlock = ^(){
            __strong typeof(weakSelf) strongSelf = weakSelf;
            [strongSelf backToSuperView];
        };
    }
    return _navBar;
}


- (SearchTableView *)resultTableView {
    if (!_resultTableView) {
        _resultTableView = [[SearchTableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.navBar.frame), SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
        _resultTableView.backgroundColor = [UIColor whiteColor];
        
        BMWeakSelf
        [_resultTableView setBlockClickCell:^(id obj) {
            if ([obj isKindOfClass:[SearchModel class]]) {
                [weakSelf gotoWorkAreaDetailVc:obj];
            }
        }];
    }
    return _resultTableView;
}

- (NSMutableArray *)resultArray {
    if (!_resultArray) {
        _resultArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _resultArray;
}

- (SearchResultCollectionView *)historyView {
    if (!_historyView) {
        __weak typeof(self) weakSelf = self;
        _historyView = [[SearchResultCollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) tagColor:[UIColor lightGrayColor] tagBgColor:[UIColor clearColor] tagBlock:^(NSString *key) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            [strongSelf searchText:key];
        }];
        _historyView.bgColor = Color_Background;
    }
    return _historyView;
}
@end
